package org.edu.modules.research.rproject.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.edu.modules.research.rproject.dto.VeJkyProjectWorkDto;
import org.edu.modules.research.rproject.entity.VeJkyProjectWork;
import org.edu.modules.research.rproject.service.VeJkyProjectWorkService;
import org.edu.common.api.vo.Result;
import org.edu.common.base.PageDto;
import org.edu.common.dto.StatusDto;
import org.edu.common.aspect.annotation.AutoLog;
import org.edu.common.system.query.QueryGenerator;
import org.edu.modules.research.rproject.vo.ProgressVo;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

@Api(tags = {"科研项目工作表"})
@RestController
@RequestMapping("/research/veJkyProjectWork")
@Slf4j
public class VeJkyProjectWorkController{

    @Autowired
    private VeJkyProjectWorkService veJkyProjectWorkService;

    @ApiOperation("分页查询科研项目工作表")
    @PostMapping("/page")
    public Result<?> page(@RequestBody PageDto<VeJkyProjectWork> page) {
        QueryWrapper<VeJkyProjectWork> queryWrapper =  page.buildQueryWrapper(VeJkyProjectWork.class);


        if(page.getParams()!=null && page.getParams().containsKey("userId")){
            queryWrapper.apply(" d.id in( select work_id from ve_jky_project_work_user where jky_user_id={0} ) ",
                    page.getParams().get("userId"));
        }

        return Result.OK(veJkyProjectWorkService.pageDto(page, queryWrapper));
    }

    @AutoLog(value = "科研项目工作表-分页列表查询")
    @ApiOperation(value="科研项目工作表-分页列表查询", notes="科研项目工作表-分页列表查询")
    @PostMapping(value = "/list")
    public Result<?> queryPageList(VeJkyProjectWork entity,
                                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<VeJkyProjectWork> queryWrapper = QueryGenerator.initQueryWrapper(entity, req.getParameterMap());
        PageDto<VeJkyProjectWork> page = new PageDto<VeJkyProjectWork>(pageNo, pageSize);
        IPage<VeJkyProjectWork> pageList = veJkyProjectWorkService.page(page, queryWrapper);
        return Result.OK(pageList);
    }


    @AutoLog(value = "通过id查询")
    @ApiOperation(value="通过id查询", notes="通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
        VeJkyProjectWork dto = veJkyProjectWorkService.findDtoById(id);
        if(dto==null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(dto);
    }

    @AutoLog(value = "添加")
    @ApiOperation(value="添加", notes="添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody VeJkyProjectWorkDto dto) {
        veJkyProjectWorkService.insertOrUpdate(dto , true);
        return Result.OK("添加成功！");
    }

    @AutoLog(value = "编辑")
    @ApiOperation(value="编辑", notes="编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody VeJkyProjectWorkDto dto) {
        veJkyProjectWorkService.insertOrUpdate(dto , false);
        return Result.OK("编辑成功!");
    }

    @AutoLog(value = "批量删除")
    @ApiOperation(value="批量删除", notes="批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
        veJkyProjectWorkService.delete(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }


    @AutoLog(value = "更新状态")
    @ApiOperation(value="更新状态")
    @PostMapping(value = "/updateStatus")
    public Result<?> updateStatus(@RequestBody StatusDto statusDto) {
        UpdateWrapper<VeJkyProjectWork> updateWrapper = new UpdateWrapper<>();
        if(statusDto.getIds()!=null && statusDto.getIds().size()>0){
            updateWrapper.set("status",statusDto.getStatus());
            updateWrapper.in("id",statusDto.getIds());
            veJkyProjectWorkService.update(updateWrapper);
            return Result.OK("批量更新成功!");
        }else{
            return Result.error("未选择!");
        }
    }


    @AutoLog(value = "更新进度")
    @ApiOperation(value="更新进度")
    @PostMapping(value = "/updateProgress")
    @Transactional
    public Result<?> updateProgress(@RequestBody List<ProgressVo> progressVo) {
        if(progressVo!=null && progressVo.size()>0){
            for (ProgressVo pvo: progressVo) {
                UpdateWrapper<VeJkyProjectWork> updateWrapper = new UpdateWrapper<>();
                updateWrapper.set("progress",pvo.getProgress());
                updateWrapper.in("id",pvo.getId());
                veJkyProjectWorkService.update(updateWrapper);
            }
            return Result.OK("更新进度成功!");
        }else{
            return Result.error("未选择!");
        }
    }




}
